import 'package:flutter/material.dart';
import 'package:flutter_cwhisky/bean/good_list_entity.dart';
import 'package:flutter_cwhisky/bean/mall_home_entity.dart';
import 'package:flutter_cwhisky/component/base/base_controller.dart';
import 'package:flutter_cwhisky/net/http_service.dart';
import 'package:flutter_cwhisky/utils/log_utils.dart';
import 'package:get/get.dart';

import '../../../../bean/good_entity.dart';
import '../../../../common/constant/api.dart';

class ChoiceLogic extends BaseController with GetTickerProviderStateMixin {
  List<MallHomeDataAdvert> banners = [];

  /// 公告
  MallHomeDataAdvert? announcement;

  /// 一号广告位
  MallHomeDataAdvert? adv1;

  /// 二号广告位
  MallHomeDataAdvert? adv2;

  /// 三号广告位
  MallHomeDataAdvert? adv3;

  List<MallHomeDataClassify> topMenu = [];

  /// 会员专区数据
  MallHomeDataMemberArea? memberArea;

  /// 独立的四个产区
  List<MallHomeDataPrefecture>? prefecture;

  /// 其他的产区
  List<MallHomeDataPrefecture>? otherPrefecture;
  List<MallHomeDataRecommend>? recommend;
  MallHomeDataRecommend? currentRecommend;
  List<GoodEntity>? goodList;

  //滑动控制器
  late ScrollController scrollController;
  bool showGoTop = false;
  double startPos = -1.0;
  double endPos = 0.0;
  late Animation<double> animation;
  late AnimationController animationController;
  int recommendIndex = 0;

  fetchHome() async {
    var response = await api.request(
      Api.mallHome,
      method: HttpService.methodGet,
    );
    if (response.isSuccess) {
      var data = MallHomeEntity.fromJson(response.data).data;
      _initHomeData(data);
    }
  }

  @override
  void onInit() {
    super.onInit();
    scrollController = ScrollController();
    animationController = AnimationController(duration: const Duration(milliseconds: 500), vsync: this);
    Animation<double> curve = CurvedAnimation(parent: animationController, curve: Curves.easeOut);
    animation = Tween(begin: -84.0, end: 0.0).animate(curve)
      ..addListener(() {
        update(["goTop"]);
      });
    scrollController.addListener(() {
      // print("offset=${scrollController.offset}==");
      // if (showGoTop != scrollController.offset > 100) {
      //
      //   showGoTop = scrollController.offset > 100;
      //   if(showGoTop){
      //     animationController.reset();
      //     animationController.forward();
      //   }else {
      //     animationController.reverse();
      //   }
      // }
    });
    fetchHome();
  }

  void _initHomeData(MallHomeData? data) {
    _clear();
    data?.advert?.forEach((element) {
      switch (element.type) {
        // 轮播图
        case 6:
          banners.add(element);
          break;
        // 一号广告位
        case 10:
          adv1 = element;
          break;
        // 二号广告位
        case 7:
          adv2 = element;
          break;
        // 三号广告位
        case 16:
          adv3 = element;
          break;
        case 13:
          announcement = element;
          break;
      }
    });
    topMenu.addAll(data!.classify!.sublist(1, 5));
    topMenu.add(data.classify![0]);
    memberArea = data.memberArea;
    if (data.prefecture!.length >= 4) {
      prefecture = data.prefecture!.sublist(0, 4);
      otherPrefecture = data.prefecture!.sublist(4);
    } else {
      prefecture = data.prefecture;
    }
    recommend = data.recommend;

    update();
    if (recommend != null && recommend!.isNotEmpty) {
      currentRecommend = recommend![0];
      _fetchGood();
    }
  }

  void _clear() {
    banners.clear();
    topMenu.clear();
  }

  void clickRecommend(int index) {
    recommendIndex = index;
    update(['recommendList']);
  }

  void _fetchGood() async {
    var response = await api.request(Api.goodList,
        method: HttpService.methodPost,
        params: {"from": 1, "size": 20, "current": 1, "classifyCode": currentRecommend!.code});
    if (response.isSuccess) {

      goodList = GoodListEntity.fromJson(response.data).data!.records;
      print('怎么回事呢=${goodList}');
      update(['recommendGoodList']);
    }
  }
}
